package com.tiancheng.trade.payment.controller.api;


import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.payment.service.PaypalOrderService;
import com.tiancheng.trade.payment.vo.api.CloseOrderVO;
import com.tiancheng.trade.payment.vo.api.PaypalCreateReqVO;
import com.tiancheng.trade.payment.vo.api.PaypalExecuteReqVO;
import com.tiancheng.trade.payment.vo.api.PaypalRefundReqVO;
import com.tiancheng.trade.payment.vo.api.res.PaypalCreateResVO;
import com.tiancheng.trade.payment.vo.api.res.PaypalExecuteResVO;
import com.tiancheng.trade.payment.vo.api.res.PaypalRefundResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * Paypal支付
 */
@RestController
@RequestMapping("api/paypal")
@Validated
@Slf4j
public class PaypalApiController {

    @Autowired
    private PaypalOrderService paypalOrderService;


    //创建支付
    @PostMapping("/creatPay")
    public Result createPay(@RequestBody PaypalCreateReqVO reqVO ){
        log.info("创建paypal支付订单数据:{}", JsonUtil.toJsonString(reqVO));
        PaypalCreateResVO paypalCreateResVO = paypalOrderService.createPayment(reqVO);
        return  Result.success(paypalCreateResVO);
    }

    //执行支付
    @PostMapping("/executePay")
    public Result successPay(@RequestBody PaypalExecuteReqVO reqVO){

        PaypalExecuteResVO paypalExecuteResVO  = paypalOrderService.executePayment(reqVO);
        return  Result.success(paypalExecuteResVO);
    }

    //关闭订单流水
    @PostMapping("/closeOrder")
    public Result closeIntFlow(@RequestBody @Valid CloseOrderVO closeOrderVO) {
        log.info("关闭订单:{}", JsonUtil.toJsonString(closeOrderVO));
        return paypalOrderService.closeOrder(closeOrderVO);
    }


    //paypal退款
    @PostMapping("/paypalRefund")
    public Result paypalRefund(@RequestBody PaypalRefundReqVO paypalRefundVO){
        log.info("退款入参:{}", JsonUtil.toJsonString(paypalRefundVO));
        PaypalRefundResVO refundRes=paypalOrderService.refundOrder(paypalRefundVO);
        return Result.success(refundRes);
    }


    /**
     * 查询paypal支付信息:  主动向paypal支付平台发起查询
     * @param paymentId
     * @return
     */
    @GetMapping("/getPaypalInfo")
    public Result getPaypal(@RequestParam("payment_id") String paymentId){
        log.info("查询paypal支付信息入参，paymentId :{}", paymentId);
        return  paypalOrderService.getPaypal(paymentId);
    }

}
